package com.adtec.base.cloud.wechatservice.wechatcloud.wechat.controller;

import com.adtec.base.cloud.commons.exception.AjaxResponse;
import com.adtec.base.cloud.commons.exception.CustomException;
import com.adtec.base.cloud.commons.exception.CustomExceptionType;
import com.adtec.base.cloud.wechatservice.wechatcloud.wechat.service.EnterpriseWeChatService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author 86185
 */
@RestController
@RequestMapping("/hospitalWeChat")
@Tag(name = "hospitalWeChat", description = "企业微信发送请求相关接口")
public class EnterpriseWeChatController {
    @Resource
    private EnterpriseWeChatService enterpriseWeChatService;






    @PostMapping(value = "/sendInfo")
    @Operation(description = "向应用发送文本消息")
    @Parameters({
            @Parameter(name = "appId", description = "要发送的应用ID(可在企业微信查询)", required = true),
            @Parameter(name = "touser", description = "指定接收消息的成员,特殊情况：指定为\"@all\"，则向该企业应用的全部成员发送"),
            @Parameter(name = "toparty", description = "指定接收消息的部门"),
            @Parameter(name = "totag", description = "指定接收消息的标签"),
            @Parameter(name = "info", description = "消息")
    })
    public AjaxResponse sendInfo(@RequestParam(name = "appId") Integer appId,
                                 @RequestParam(name = "touser", required = false) String touser,
                                 @RequestParam(name = "toparty", required = false) String toparty,
                                 @RequestParam(name = "totag", required = false) String totag,
                                 @RequestParam(name = "info") String info) {
//       没有选择应用ID时使用默认ID发送
        if (appId == null) {
            appId = 1000002;
//            throw new CustomException(CustomExceptionType.USER_INPUT_ERROR, "要发送的应用ID不能为空");
        }
        //三个接收方都为空时默认全部发送
        if (StringUtils.isBlank(touser) && StringUtils.isBlank(toparty) && StringUtils.isBlank(totag)) {
            touser = "@all";
        }
        if (StringUtils.isBlank(info)) {
            throw new CustomException(CustomExceptionType.USER_INPUT_ERROR, "不能发送空消息");
        }

        String msgid = enterpriseWeChatService.sendInfo(appId, touser, toparty, totag, info);
        return AjaxResponse.success(msgid, "发送成功");
    }

    @PostMapping(value = "/recallMessage")
    @Operation(description = "撤回消息")
    @Parameters({@Parameter(name = "msgid", description = "消息id")})
    public AjaxResponse recallMessage(@RequestParam("msgid") String msgid) {
        if (StringUtils.isBlank(msgid)) {
            throw new CustomException(CustomExceptionType.USER_INPUT_ERROR, "消息ID不能为空");
        }
        String recallMessage = enterpriseWeChatService.recallMessage(msgid);
        return AjaxResponse.success(recallMessage);
    }

}
